package com.ts.opengl.IInterface

import android.R
import android.opengl.GLSurfaceView
import android.opengl.Matrix
import android.util.Log

abstract class TransformRender: GLSurfaceView.Renderer {
    var m_AngleX: Int = 0
    var m_AngleY: Int = 0
    var m_ScaleX: Float = 0f
    var m_ScaleY: Float = 0f
    private  val TAG = "TransformRender"

    var program=0;
    var fboProgram=0;

    var Projection=FloatArray(16)
    var view=FloatArray(16)

     var mViewMatrix = FloatArray(16)
     var mProjectMatrix = FloatArray(16)
     var mMVPMatrix = FloatArray(16)
    fun getOriginalMatrix(): FloatArray {
        return floatArrayOf(
            1f, 0f, 0f, 0f,
            0f, 1f, 0f, 0f,
            0f, 0f, 1f, 0f,
            0f, 0f, 0f, 1f
        )
    }

    var model=getOriginalMatrix()
    fun UpdateMVPMatrix(angleX:Int,angleY: Int,ratio:Float){
        Log.e(TAG, "旋转角度1 $angleX---$angleY")
        if (angleX==0||angleY==0){
            return
        }
        val angleX1=angleX%360.0
        val angleY1=angleY%360.0
        Log.e(TAG, "旋转角度3232 $angleX1 $angleY1")
        val radiansX=(Math.PI/180.0f*(angleX1)).toFloat()
        val radiansY=(Math.PI/180.0f*(angleY1)).toFloat()
        Log.e(TAG, "旋转角度3232 $radiansX $radiansY")

        //计算宽高比
        val ratio = R.attr.width.toFloat() / R.attr.height

        //设置透视投影
        Matrix.perspectiveM(mProjectMatrix, 0,45.0f,ratio,0.1f,200.0f)

        //设置相机位置
        Matrix.setLookAtM(mViewMatrix, 0, 0f, 0f, 3f, 0f, 0f, 0f, 0f, 1.0f, 0.0f)
        Log.e(TAG, "旋转角度322232 $m_ScaleX $m_ScaleX  $radiansX  $radiansY")
        Matrix.scaleM(model,0,m_ScaleX,m_ScaleX,1.0f)
        Matrix.rotateM(model,0, radiansX,1.0f,0.0f,0.0f)
        Matrix.rotateM(model,0, radiansY,0.0f,1.0f,0.0f)
        Matrix.translateM(model, 0,0.0f, 0.0f, 0.0f)
        //计算变换矩阵
        Matrix.multiplyMM(mMVPMatrix, 0, mProjectMatrix, 0, mViewMatrix, 0)
        Matrix.multiplyMM(mMVPMatrix, 0, mMVPMatrix, 0, model, 0)
    }


    open fun UpdateTransformMatrix(rotateX:Float, rotateY:Float, scaleX:Float, scaleY:Float){
        Log.e(TAG, "旋转角度12 $rotateX---$rotateY")
        m_AngleX=rotateX.toInt()
        m_AngleY=rotateY.toInt()
        m_ScaleY=scaleY
        m_ScaleX=scaleX

        Log.e(TAG, "旋转角度1222 $m_AngleX---$m_AngleY")

    }
}